/*
* Copyright (c) 2012 Kyle Hayward
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#pragma once

namespace OptixDotNet 
{
	ref class Program;
	
	/// <summary>
	/// A geometry node is the fundamental to describe a geometric object: a collection of user-defined primitives 
	/// against which rays will be intersected.
	/// </summary>
	public ref class Geometry : public OptixNode, IVariableContainer
	{
	public:

		Geometry( Context^ context );

		/// <summary>
		/// Checks to make sure the object is valid and consistent
        /// </summary>
		virtual void Validate() override
		{
			CheckError( rtGeometryValidate( mGeo ) );
		}

		/// <summary>
		/// Destroys the internal Optix resource
        /// </summary>
		virtual void Destroy() override
		{
			if( mGeo )
				CheckError( rtGeometryDestroy( mGeo ) );

			mGeo = 0;
		}

		/// <summary>
		/// Invalidates the geometry node and notifies Optix that it needs to be rebuilt
        /// </summary>
		void MarkAsDirty()
		{
			CheckError( rtGeometryMarkDirty( mGeo ) );
		}

		/// <summary>
		/// Queries the geometry node to determine if it has been marked as dirty
        /// </summary>
		bool IsDirty()
		{
			int dirty;
			CheckError( rtGeometryIsDirty( mGeo, &dirty ) );

			return dirty == 1;
		}

		/// <summary>
		/// Set the number of primitives for the geometry
        /// </summary>
		property unsigned int PrimitiveCount
		{
			unsigned int get()
			{
				unsigned int count;
				CheckError( rtGeometryGetPrimitiveCount( mGeo, &count ) );

				return count;
			}
			void set( unsigned int count )
			{
				CheckError( rtGeometrySetPrimitiveCount( mGeo, count ) );
			}
		}

		/// <summary>
		/// Set the program that will run when a ray collides with geometry
        /// </summary>
		property Program^ IntersectionProgram
		{
			Program^ get();
			void set( Program^ program );
		}

		/// <summary>
		/// Set the bounding box program
        /// </summary>
		property Program^ BoundingBoxProgram
		{
			Program^ get();
			void set( Program^ program );
		}

		/// <summary>
		/// Gets the number of global variables on the Context
        /// </summary>
		property int VariableCount
		{
			virtual int get()
			{
				unsigned int count;
				CheckError( rtGeometryGetVariableCount( mGeo, &count ) );
				return count;
			}
		}

		/// <summary>
		/// Gets a local variable on the geometry with name [s]. If the variable doesn't exist, it is created.
		/// This is mainly used for defining the geometry buffers for the geometry (e.g. vertex, index, etc buffers)
        /// </summary>
		property Variable^ default[System::String^]
		{
			virtual Variable^ get( System::String^ s );
			virtual void set( System::String^s, Variable^ v );
		}

		/// <summary>
		/// Gets a local geometry variable at index [index]
        /// </summary>
		property Variable^ default[int]
		{
			virtual Variable^ get ( int index );
			virtual void set( int index, Variable^ v );
		}

	internal:
		Geometry( Context^ context, RTgeometry geom );
		inline RTgeometry InternalPtr() { return mGeo; }

	private:

		RTgeometry	mGeo;
	};
}